<?php
	//保存配置信息
function updateconfig($config){
		$config_old = require './App/Conf/config.php';

		if(is_array($config) && is_array($config_old)){
			$config_new = array_merge($config_old,$config);
		}
		if(is_array($config_new)){
			arr2file('./App/Conf/config.php',$config_new);
		}
		@unlink('./App/Runtime/~runtime.php');
	}

	//获取SQL脚本
function get_sql($sql_file){
		
		$contents = file_get_contents($sql_file);
		$contents = str_replace("\r\n", "\n", $contents);
		$contents = trim(str_replace("\r", "\n", $contents));
		$return_items = $items = array();
		$items = explode(";\n", $contents);
		foreach ($items as $item) {
			$return_item = '';
			$item = trim($item);
			$lines = explode("\n", $item);
			foreach ($lines as $line) {
				if (isset($line[1]) && $line[0] . $line[1] == '--') {
					continue;
				}
				$return_item .= $line;
			}
			if ($return_item) {
				$return_items[] = $return_item;
			}
		}
		return $return_items;
	}
	
function isfun($f){
  return (false!==function_exists($f))? TRUE:FALSE;
}	

// 获取相对目录
function get_base_path($filename){
	$base_path = $_SERVER['PHP_SELF'];
	$base_path = substr($base_path,0,strpos($base_path,$filename));
	return $base_path;
}

function is_email($email){
		
		$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,5}\$/i";
		if (strpos($email, '@') !== false && strpos($email, '.') !== false) {
			if (preg_match($chars, $email)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

// 数组保存到文件
function arr2file($filename, $arr=''){
	if(is_array($arr)){
		$con = var_export($arr,true);
	} else{
		$con = $arr;
	}
	$con = "<?php\nreturn $con;\n?>"; //生成配置文件内容

	$dir = dirname($filename);
	if(!is_dir($dir)){
		mkdir($dir);
	}
	return @file_put_contents($filename,$con); //写入./config.php中
}

?>